package com.heima.article.service.impl;

import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ArticleHomeService;
import com.heima.common.constans.ArticleConstants;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ArticleHomeServiceImpl implements ArticleHomeService {

    @Resource
    private ApArticleMapper apArticleMapper;
    /**
     * 定义一个service接口即可实现首页，上拉，下拉
     *
     * @param dto
     * type表示类型，==1表示上拉，更多，==2表示下拉，最新
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto,Short type) {
        //1.参数校验
        if(dto.getTag()==null){
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        if(dto.getSize()==null){
            dto.setSize(10);
        }
        if(dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }
        if(dto.getMinBehotTime()==null){
            dto.setMinBehotTime(new Date());
        }
        if(type==null){
            type= ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        //2.调用mapper接口
        List<ApArticle> apArticles = apArticleMapper.loadArticleList(dto, type);

        //3.返回数据
        return ResponseResult.okResult(apArticles);
    }
}
